﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using XemlUtilities.DataSet;

namespace XemlUtilities.ISA
{
    class MetaboliteProfile
    {
        public static MemoryStream GetStream(Guid xemlId, string connectionstring)
        {
            MemoryStream res = new MemoryStream();
            System.IO.StreamWriter txtWriter = new System.IO.StreamWriter(res);
            using (gmdProfileService.XemlProfile.IsaExportDataTable dt = new gmdProfileService.XemlProfile.IsaExportDataTable(xemlId))
            {
                dt.GetXemlExperimentHeatMapData(connectionstring);

                txtWriter.WriteLine(string.Join(IsaExporter.multipleFieldDelimiter, IsaExporter.Encapsulate(dt.Columns.Cast<DataColumn>().Select(x => x.Caption).Select(c => c.ToString()).ToList())));
                foreach (DataRow r in dt.Rows)
                {
                    txtWriter.WriteLine(string.Join(IsaExporter.multipleFieldDelimiter, IsaExporter.Encapsulate(r.ItemArray.Select(c => c.ToString()).ToList())));
                }
                txtWriter.Flush();

                res.Seek(0, SeekOrigin.Begin);
                return res;
            }
        }

        public static string GetFileName(XemlDataSet xds)
        {
            return string.Format("m_{0}.tsv", xds.Document.Single().PutativeFileName);
        }
    }
}
